package com.bsierp.servicebean.printsverice;
import java.util.List;


import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import com.bsierp.business.pojo.Order;
import com.bsierp.business.pojo.User;

import com.bsierp.util.hibernate.HibernateSessionFactory;



public class OrderPrintService implements PrintService {
 
    public String printStr;

    /* (non-Javadoc)
     * @see com.bsierp.servicebean.printobject.PrintService#getPrintStr(Integer[] id)
     */
    
    public String getPrintStr(String[] id,User u,String contextPath) {
       
        StringBuffer hql=new StringBuffer();
        StringBuffer retVal=new StringBuffer();
        hql.append("from Order o where o.userByOrderSeller=:userByOrderSeller and o.orderId in(");
        int k=0; 
        for(int i=0;i<id.length;i++){
             if(k==0){
                 hql.append(id[i]);
             }else{
                 hql.append(","+id[i]);
             }
             k++;
         }
        
        hql.append(")");
        SessionFactory sessionFactory=HibernateSessionFactory.getSessionFactory();
        Session session=sessionFactory.openSession();
        Transaction tx=null;
        try{
            tx=session.beginTransaction();
            Query q=session.createQuery(hql.toString());
            q.setEntity("userByOrderSeller", u);
            List l=q.list();
            retVal.append("<table >");
           
            
            int c=0;//初始列数
            int maxC=4;//表格最大列数
            for(int i=0;i<l.size();i++){
                Order o=(Order)l.get(i);
                for(int on=1;on<o.getOrderAmount()+1;on++){//遍订单产品数量，如一个订单有N个产品则需要生成N张格式为 订单号+“-”+第几件+“X”的条形码订单
                if(c==0){
                    retVal.append("<tr>");  
                }
                retVal.append("<th>");
                retVal.append("<img src="+contextPath+"/barcode?msg="+o.getOrderId()+"-"+on+"X"+"&type=code39   height=50px width=300px ><br/>");
                retVal.append("产品型号："+o.getProduct().getProductModel()+"<br/>");
                retVal.append("数量："+o.getOrderAmount()+"<br/>");
                retVal.append("用户："+o.getUserByOrderBuyer().getUserName()+"<br/>");
                retVal.append("订购日期："+o.getOrderBookdate()+"<br/>");
                retVal.append("交货日期："+o.getOrderDeadline()+"<br/>");
                retVal.append("规格要求："+o.getOrderRequest()+"<br/>");
                retVal.append("备注："+o.getOrderRemarks()+"<br/>");
                retVal.append("裁剪_______车位_______手工_______质检_______");
                retVal.append("</th>");
                c++;
                if(c==maxC){
                    retVal.append("</tr>");
                    c=0;
                }
                }
            }
            
            retVal.append("</table >");
        }catch(Exception e){
            tx.rollback();
        }finally{
            session.close();
        }
               return retVal.toString();
    }


    
}
